package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        Page<Dish> page = new Page<>();
        page.setCurrent(dto.getPage());
        page.setSize(dto.getPageSize());
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(dto.getName() != null, Dish::getName, dto.getName())
                .eq(dto.getCategoryId() != null, Dish::getCategoryId, dto.getCategoryId())
                .eq(dto.getStatus() != null, Dish::getStatus, dto.getStatus());
        Page<Dish> pageQuery = this.page(page, lambdaQueryWrapper);
        return new PageResult(pageQuery.getTotal(), pageQuery.getRecords());


    }

    @Override
    public void add(DishDTO dto) {
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        this.save(dish);
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }

    }

    @Override
    public DishVO getdishById(Integer id) {
        Dish dish = this.getById(id);
        DishVO dishVO = BeanUtil.copyProperties(dish, DishVO.class);
        List<DishFlavor> flavors = dishFMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void updatecaipin(DishDTO dto) {

        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        this.updateById(dish);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFMapper.delete(lambdaQueryWrapper);
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }


    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(List ids) {
        dishMapper.selectBatchIds(ids);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void status(Integer status, Integer id) {

        Dish dish = new Dish();
        dish.setId(Long.valueOf(id));
        dish.setStatus(status);
        this.updateById(dish);

    }

    @Override
    public List<Dish> getlistId(Integer categoryId) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, categoryId);
        lambdaQueryWrapper.eq(Dish::getStatus, 1);
        List<Dish> list = this.list(lambdaQueryWrapper);
        return list;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
